package edu.olin.scheduler.gui.schedule;

import edu.olin.scheduler.common.*;
import edu.olin.scheduler.gui.*;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class TimeLine extends JPanel implements GUIPrefs.Listener {

    private JLabel dayLabel;
    private ArrayList<JLabel> timeLabels;
    
    public TimeLine(){
        super(true);
        GUIPrefs.addListener(this);
        setLayout(null);
        setBackground(GUIPrefs.getMarginBGColor());
        dayLabel = new JLabel(" ");
        dayLabel.setBackground(GUIPrefs.getMarginBGColor());
        dayLabel.setForeground(GUIPrefs.getMarginFGColor());
        dayLabel.setFont(GUIPrefs.getDayOfWeekFont());
        dayLabel.setOpaque(true);
        add(dayLabel);
        setOpaque(true);
        rebuildLabels();
        }
    
    public void guiPrefsChanged(){
        dayLabel.setBackground(GUIPrefs.getMarginBGColor());
        dayLabel.setForeground(GUIPrefs.getMarginFGColor());
        dayLabel.setFont(GUIPrefs.getDayOfWeekFont());
        setBackground(GUIPrefs.getMarginBGColor());
        rebuildLabels();
        repaint();
        }
    
    private void rebuildLabels(){
        if(timeLabels==null){ timeLabels = new ArrayList(); }
        Iterator<JLabel> itr = timeLabels.iterator();
        while(itr.hasNext()){ remove(itr.next()); }
        timeLabels.clear();
        for(int i=GUIPrefs.getStart().hours(); i<GUIPrefs.getEnd().hours(); i++){
            String s = i+":00";
            if(!GUIPrefs.is24hour()){
                int i12 = (i%12==0?12:i%12);
                s = i12+":00";
                if(i==GUIPrefs.getStart().hours()||i==12){
                    s = i12+" "+(i<12?"AM":"PM");
                    }
                }
            JLabel label = new JLabel(s,JLabel.RIGHT);
            label.setName(i+"");
            label.setFont(GUIPrefs.getMajorTimeFont());
            label.setBackground(GUIPrefs.getMarginBGColor());
            label.setForeground(GUIPrefs.getMarginFGColor());
            label.setOpaque(false);
            this.add(label);
            timeLabels.add(label);
            }
        doLayout();
        }
    
    public void doLayout(){
        super.doLayout();
        Iterator<JLabel> itr = timeLabels.iterator();
        int maxw = getWidth();
        while(itr.hasNext()){
            JLabel next = itr.next();
            int h = -1;
            try{ h=Integer.parseInt(next.getName()); }catch(NumberFormatException e){System.err.println("    "+next);continue;}
            next.setLocation(5,getYPosition(new SimpleTime(h,00)));
            next.setSize(getWidth()-10,next.getPreferredSize().height);
            if(next.getPreferredSize().width>maxw){
                maxw = next.getPreferredSize().width;
                }
            }
        if(maxw>getWidth()){
            setPreferredSize(new Dimension(maxw+10,getPreferredSize().height));
            }
        }
    
    public void paint(Graphics g){
        super.paint(g);
        //draw major/minor lines
        LineDrawer.drawLines(g,5,getWidth()-5,GUIPrefs.getHeaderSize(),getHeight(),false,GUIPrefs.getMarginFGColor());
        //draw in the day label
        dayLabel.setLocation(0,0);
        dayLabel.setSize(getWidth(),GUIPrefs.getHeaderSize());
        //draw all of the subcomponents
        paintComponents(g);
        }
    
    //at what y-position on this SingleDayDisplay is the given SimpleTime?
    private int getYPosition(SimpleTime st){
        return LineDrawer.getYPosition(st,dayLabel.getHeight(),getHeight());
        }
    
    }
